﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using University.Workshop.DataAccess;
using University.Workshop.Model;
using System.Threading;
using System.Globalization;
using University.Workshop.DataAccess.LanguagePack.ClientLanguagePack;

namespace University.Workshop.ClientApp
{
    /// <summary>
    /// Interaction logic for GaleryConfirm.xaml
    /// </summary>
    public partial class GaleryConfirm : Window
    {
        private string LanguageSelected;
        private string MessageErrorInsufficientMoney;
        private string MessageChipsAdded;


        Client client = new Client();
        User user;
        string saleOrBuy;
        int costFigure;
        int numberBicho;

        public GaleryConfirm()
        {
            InitializeComponent();
        }
        public GaleryConfirm(User userReceived,string buyOrSale,int cost,int number,string deck)
        {
            InitializeComponent();
            user = userReceived;
            saleOrBuy = buyOrSale;
            costFigure = cost;
            numberBicho = number;

            if (saleOrBuy == "buy")
            {
                buttonBuy.IsEnabled = true;
                buttonsale.IsEnabled = false;
                labelSumOrRes.Content = "- " + costFigure;
            }
            else
            {
                buttonBuy.IsEnabled = false;
                buttonsale.IsEnabled = true;
                labelSumOrRes.Content = "+ " + costFigure;
            }
            BitmapImage bitMap = new BitmapImage();
            bitMap.BeginInit();
            bitMap.UriSource = new Uri(deck, UriKind.Relative);
            bitMap.EndInit();

            image1.Source = bitMap;


            labelUserName.Content = user.Name;
            labelUserChips.Content = user.Chips;

        }

        public GaleryConfirm(User userReceived, string buyOrSale, int cost, int number, string deck, string languageSelected)
        {
            InitializeComponent();
            this.LanguageSelected = languageSelected;
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(this.LanguageSelected);
            ApplyLanguage();

            user = userReceived;
            saleOrBuy = buyOrSale;
            costFigure = cost;
            numberBicho = number;

            if (saleOrBuy == "buy")
            {
                buttonBuy.IsEnabled = true;
                buttonsale.IsEnabled = false;
                labelSumOrRes.Content = "- " + costFigure;
            }
            else
            {
                buttonBuy.IsEnabled = false;
                buttonsale.IsEnabled = true;
                labelSumOrRes.Content = "+ " + costFigure;
            }
            BitmapImage bitMap = new BitmapImage();
            bitMap.BeginInit();
            bitMap.UriSource = new Uri(deck, UriKind.Relative);
            bitMap.EndInit();

            image1.Source = bitMap;


            labelUserName.Content = user.Name;
            labelUserChips.Content = user.Chips;

        }

        private void ApplyLanguage()
        {
            this.label2.Content = ClientLanguagePack.GalleryConfirmLabelUser;
            this.label3.Content = ClientLanguagePack.GalleryConfirmLabelChips;

            this.buttonBuy.Content = ClientLanguagePack.GalleryConfirmButtonBuy;
            this.buttonsale.Content = ClientLanguagePack.GalleryConfirmButtonSale;
            this.button2.Content = ClientLanguagePack.GalleryConfirmButtonCancel;

            this.MessageChipsAdded = ClientLanguagePack.GalleryConfirmMessageChipsAdded;
            this.MessageErrorInsufficientMoney = ClientLanguagePack.GalleryConfirmMessageErrorInsufficientMoney;
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

/*        private void button1_Click(object sender, RoutedEventArgs e)
        {
            client.Send("99 " + user.Name + " " + numberBicho);
            string resultfour = client.Receive();
        }*/

        private void button1_Click(object sender, RoutedEventArgs e)
        {
                if ( (user.Chips) >= costFigure) //suficiente dinero y compra el vicho
                {
                    client.Send("99 " + user.Name + " " + numberBicho);
                    string resultfour = client.Receive();
                    user.Decks.Add(numberBicho);
                    user.Chips = user.Chips - costFigure;

                    client.Send("14 "+user.Name + " " +(user.Chips ) );
                    client.Receive();
                }
                else
                {
                    MessageBox.Show(this.MessageErrorInsufficientMoney);
                }
                this.Close(); 
        }

        private void buttonsale_Click(object sender, RoutedEventArgs e)
        {
            client.Send("97 " + user.Name + " " + numberBicho);
            string resultfour = client.Receive();
            user.Decks.Remove(numberBicho);
            user.Chips = user.Chips + costFigure;

            client.Send("14 " + user.Name + " " + (user.Chips ));
            client.Receive();

            MessageBox.Show( Convert.ToInt32( costFigure )+ this.MessageChipsAdded);
            
            this.Close(); 
        }
    }
}
